<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:dswp 信息發送記錄管理
 *@param datetime 2014-01-12
 *@param author abin
 ********************/
class Sms extends Admins_Controller {
  
  private $data=array();
  private $unique_id;

  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='orders';
    $this->load->model('sms_mdl');
    $this->data['format_time']=$this->settings['default_time'];
	$this->data['lang'] =$this->lang->language;
  }

  public function index(){
    $this->_get_list();
    $this->_get_sms_statistics();
    // $this->output->enable_profiler(true);
    $this->load->view('admins/sms_list_view',$this->data);
  } 

  private function _get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    // $limit = $this->data['default_admin_limit'];
    $limit =30;
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admin/sms');
    }
    $url_query=$_SERVER['QUERY_STRING'];

    // 保留查詢參數;
    $cookie = array(
        'name'   => 'url_query',
        'value'  => base64_encode($url_query),
        'expire' => '0',
        'path'   => '/',
    );
    $this->input->set_cookie($cookie);

    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }

    $this->data['phone_number']=$this->input->get('phone_number');
    $this->data['phone_area']=$this->input->get('phone_area');
    $this->data['send_result']=$this->input->get('send_result');
    $this->data['begin_month']=$this->input->get('begin_month');
    $this->data['end_month']=$this->input->get('end_month');

    
    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'id';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'desc';
    
    $data=array(
      'limits'=>$limit,
      'offset'=>$offset,
      'field'=>$this->data['field'],
      'sort'=>$this->data['sort'],
      'phone_number'=>$this->data['phone_number'],
      'phone_area'=>$this->data['phone_area'],
      'send_result'=>$this->data['send_result'],
      'begin_month'=>!empty($this->data['begin_month'])?strtotime($this->data['begin_month']):'',
      'end_month'=>!empty($this->data['end_month'])?strtotime($this->data['end_month']):'',
    );

    $this->data['lists_count']=$this->sms_mdl->totle_sms($data);
    
    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->changeClass('pagination');
      // $this->dpagination->showCounter(TRUE);
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(2);
      $this->dpagination->target(site_url('admins/sms').'?'.$url_query);
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
      $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->sms_mdl->get_sms($data);
  }

  //短信發送統計
  private function _get_sms_statistics(){
    $setting=array(
      'group'=>'other',
      'key'=>'sms_count'
    );
    $query=$this->settings_mdl->get_settings_by_where($setting);
    $this->data['total_sms']=$query['value'];
    //今天
    $today_begin = date("Y-m-d 00:00:00");
    $today_end = date("Y-m-d 23:59:59");
    $today=array(
      'begin_month'=>strtotime($today_begin),
      'end_month'=>strtotime($today_end),
    );
    $this->data['today_sms']=$this->sms_mdl->totle_sms($today);

    //昨天
    $yest_begin = date("Y-m-d 00:00:00",strtotime('-1 day'));
    $yest_end = date("Y-m-d 23:59:59",strtotime('-1 day'));
    $yest=array(
      'begin_month'=>strtotime($yest_begin),
      'end_month'=>strtotime($yest_end),
    );
    $this->data['yest_sms']=$this->sms_mdl->totle_sms($yest);
  }

  //執行刪除功能
  public function delete($delete_id=0){
    if(!empty($delete_id)){
      $result=$this->sms_mdl->delete_sms($delete_id);
      $this->operator_log('刪除信息發送記錄->ID:'.$delete_id,'刪除',$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect('admins/sms');
    }else{
      show_error('對不起,delete_id參數出錯!');
      exit;
    }
  }

  //執行清空記錄
  public function delete_all(){
    $result=$this->sms_mdl->empty_table();
    $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
    $this->operator_log('清空信息發送記錄','清空',$result);
    $notify = $result > 0?'success':'error';
    $this->nsession->set_flashdata($notify, $msg);
    redirect("admins/sms");
  }

  //短信充值頁面
  public function recharge(){
    $setting=array(
      'group'=>'other',
      'key'=>'sms_count'
    );
    $query=$this->settings_mdl->get_settings_by_where($setting);
    $this->data['total_sms']=$query['value'];
    $this->load->view('admins/sms_recharge_view',$this->data);
  }

  //執行充值
  public function recharge_posts(){
    $recharge_num=$this->input->post('recharge_num');
    $total_sms=$this->input->post('total_sms');
    $result=0;
    if(!empty($recharge_num) && is_numeric($recharge_num)){
      $setting=array(
        'group'=>'other',
        'key'=>'sms_count',
        'value'=>($recharge_num+$total_sms)
      );
      $result=$this->settings_mdl->edit_settings_by_keyGroup($setting);
    }
    $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
    $this->operator_log('短信充值+'.$recharge_num,'充值',$result);
    $notify = $result > 0?'success':'error';
    $this->nsession->set_flashdata($notify, $msg);
    redirect("admins/sms");
  }

}